package com.atguigu.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author KJ_ICU
 * @date 2021/06/22
 */
public class AirCondition {
    int number = 0;
    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void increment() throws InterruptedException
    {
        lock.lock();
        try{
            //1 判断
            while(number != 0)
            {
                this.await();
            }
            //2 干活
            System.out.println(Thread.currentThread().getName()+"\t"+(++number));
            //3 通知
            condition.signal();
        }finally{
            lock.unlock();
        }
    }

    public synchronized void decrement() throws InterruptedException
    {
        //1 判断
        if(number == 0)
        {
            this.wait();
        }
        //2 干活
        System.out.println(Thread.currentThread().getName()+"\t"+(--number));
        //3 通知
        this.notifyAll();
    }
}
